from requests_html import HTMLSession
import os

session = HTMLSession()

class LOLsp(object):
    os_path = os.path.join(os.getcwd(), '英雄联盟')

    if not os.path.exists(os_path):
        os.mkdir(os_path)

    def __init__(self):
        self.hero_info_name = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js?ts=2897302'
        self.hero_info_img = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js?ts=2897313'

    def getdata(self):
        res = session.get(self.hero_info_name).json()
        for data1 in res['hero']:
            hero_name = data1['title']
            hero_id = data1['heroId']
            hero_folder = os.path.join(self.os_path, hero_name)

            if not os.path.exists(hero_folder):
                os.mkdir(hero_folder)

            img_url = self.hero_info_img.format(hero_id)
            info_res = session.get(img_url).json()
            self.changedata(info_res, hero_folder)

    def changedata(self, info_res, hero_folder):
        for hero_img in info_res['skins']:
            if hero_img['mainImg']:
                img_name = hero_img['name'].replace('/', '_') + '.jpg'  # 替换非法字符
                img_path = os.path.join(hero_folder, img_name)

                img_data = session.get(hero_img['mainImg']).content
                with open(img_path, 'wb') as f:
                    f.write(img_data)

if __name__ == '__main__':
    l = LOLsp()
    l.getdata()